<?php

defined('SYSPATH') OR die('No direct access allowed.');

class Controller_Comments extends Controller_Initialize {

  public $secure_actions = array(
      // user actions
      'photo_add' => 'login',
      'discussion_add' => 'login',
  );

  public function action_photo_add() {

    if (!empty($_POST) && is_numeric($_POST['pid'])) {

      $comments_photo = null;
      $comments_photo = ORM::factory('comments_photo');
      $post = $comments_photo->validate_add_comment($_POST);

      if ($post->check()) {
        $comments_photo->photo_user_id = $_POST['pid'];
        $comments_photo->username = $this->userAuth->username;
        $comments_photo->text = Text::auto_link(strip_tags($_POST['ctext'],'<cite><strong>'));
        $comments_photo->date = time();
        $comments_photo->save();
        Message::add('success', 'Komentář byl přidán.');
        Request::instance()->redirect('photo/view/'.$_POST['pid']);
      } else {
        $informations = null;
        foreach($post->errors('comments') as $item) {
          $informations .= '<span>'.$item.'</span>';
        }
        Message::add('warning', '<strong>Komentář nebyl přidán.</strong> '.$informations);
        Request::instance()->redirect($_SERVER['HTTP_REFERER']);
      }
    }
  }

  public function action_article_add() {

    if (!empty($_POST) && is_numeric($_POST['aid'])) {

      $comments_article = null;
      $comments_article = ORM::factory('comments_article');
      $post = $comments_article->validate_add_comment($_POST);

      if ($post->check()) {
        $comments_article->article_id = $post['aid'];
        $comments_article->username = $this->userAuth->username;
        $comments_article->text = Text::auto_link(strip_tags($post['atext'],'<cite><strong>'));
        $comments_article->date = time();
        $comments_article->save();
        Message::add('success', 'Komentář byl přidán.');
        Request::instance()->redirect($_SERVER['HTTP_REFERER']);
      } else {
        $informations = null;
        foreach($post->errors('comments') as $item) {
          $informations .= '<span>'.$item.'</span>';
        }
        Message::add('warning', '<strong>Komentář nebyl přidán.</strong> '.$informations);
        Request::instance()->redirect($_SERVER['HTTP_REFERER']);
      }
    }
  }

  public function action_blog_add() {

    if (!empty($_POST) && is_numeric($_POST['bid'])) {

      $comments_blog = null;
      $comments_blog = ORM::factory('comments_blog');
      $post = $comments_blog->validate_add_comment($_POST);

      if ($post->check()) {
        $comments_blog->blog_id = $post['bid'];
        $comments_blog->username = $this->userAuth->username;
        $comments_blog->text = Text::auto_link(strip_tags($post['btext'],'<cite><strong>'));
        $comments_blog->date = time();
        $comments_blog->save();
        Message::add('success', 'Komentář byl přidán.');
        Request::instance()->redirect($_SERVER['HTTP_REFERER']);
      } else {
        $informations = null;
        foreach($post->errors('comments') as $item) {
          $informations .= '<span>'.$item.'</span>';
        }
        Message::add('warning', '<strong>Komentář nebyl přidán.</strong> '.$informations);
        Request::instance()->redirect($_SERVER['HTTP_REFERER']);
      }
    }
  }

  public function action_request_add() {

    if (!empty($_POST) && !empty($_POST['rid'])) {

      $comments_request = null;
      $comments_request = ORM::factory('comments_request');
      $post = $comments_request->validate_add($_POST);

      if ($post->check()) {
        $comments_request->pattern = $_POST['rid'];
        if (isset($this->userAuth->username)) {
          $comments_request->username = $this->userAuth->username;
        } else {
          $comments_request->username = 'Nepřihlášený uživatel';
        }
        $comments_request->text = Text::auto_link(strip_tags($_POST['ctext'],'<cite><strong>'));
        $comments_request->date = time();
        $comments_request->save();
        Message::add('success', 'Váš komentář byl přidán.');
        Request::instance()->redirect('request/view/'.$_POST['rid']);
      } else {
        $informations = null;
        foreach($post->errors('comments') as $item) {
          $informations .= '<span>'.$item.'</span>';
        }
        Message::add('warning', '<strong>Komentář nebyl přidán.</strong> '.$informations);
        Request::instance()->redirect('request/view/'.$_POST['rid']);
      }
    }
  }

  public function action_discussion_add() {

    if (!empty($_POST) && is_numeric($_POST['did'])) {

      $model = null;
      $model = ORM::factory('discussions_reply');
      $post = $model->validate_add($_POST);

      if ($post->check()) {
        $model->to_id = $_POST['did'];
        $model->username = $this->userAuth->username;
        $model->text = Text::auto_link(strip_tags($_POST['text'],'<cite><strong>'));
        $model->date = time();
        $model->save();
        Message::add('success', 'Odpověď byla přidána.');
        Request::instance()->redirect('discussion/'.$_POST['did']);
      } else {
        $informations = null;
        foreach($post->errors('comments') as $item) {
          $informations .= '<span>'.$item.'</span>';
        }
        Message::add('warning', '<strong>Odpověď nebyla přidána.</strong> '.$informations);
        Request::instance()->redirect('discussion/'.$_POST['did']);
      }
    }
  }

}